package gold.contest;

import gold.utils.InputUtil;

import java.util.*;
import java.util.Arrays;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC5632 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目直接用别人代码，自己只理解思想
     * 经典解法，每日一看
     * 竞赛题目的竞赛级别代码
     * @return:
     */
    class Solution {
        public class UF {
            // 连通分量个数
            private int count;
            // 存储一棵树
            private int[] parent;
            // 记录树的“重量”
            private int[] size;

            public UF(int n) {
                this.count = n;
                parent = new int[n];
                size = new int[n];
                for (int i = 0; i < n; i++) {
                    parent[i] = i;
                    size[i] = 1;
                }
            }

            /* 将 p 和 q 连接 */
            public void union(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                if (rootP == rootQ)
                    return;

                // 小树接到大树下面，较平衡
                if (size[rootP] > size[rootQ]) {
                    parent[rootQ] = rootP;
                    size[rootP] += size[rootQ];
                } else {
                    parent[rootP] = rootQ;
                    size[rootQ] += size[rootP];
                }
                count--;
            }

            /* 判断 p 和 q 是否连通 */
            public boolean connected(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                return rootP == rootQ;
            }

            private int find(int x) {
                while (parent[x] != x) {
                    // 进行路径压缩
                    parent[x] = parent[parent[x]];
                    x = parent[x];
                }
                return x;
            }

            /* 返回图中有多少个连通分量 */
            public int count() {
                return count;
            }
        }

        public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {
            //使用TreeMap的时候不能简单的用o1[2]<o2[2]，如果返回0会直接覆盖原有的key导致数据丢失
            TreeMap<int[], Integer> map = new TreeMap<>((o1, o2) -> o1[2] != o2[2] ? o1[2] - o2[2] : 1);
            for (int i = 0; i < queries.length; i++) {
                map.put(queries[i], i);
            }
            Arrays.sort(edgeList, (o1, o2) -> o1[2] - o2[2]);
            boolean[] ans = new boolean[queries.length];
            UF uf = new UF(n);
            int index = 0;  //记录上一次query结果，已经用到哪条边了

            while (!map.isEmpty()) {
                Map.Entry<int[], Integer> curQueryNode = map.pollFirstEntry();
                int[] query = curQueryNode.getKey();
                int queryIndex = curQueryNode.getValue();
                for (; index < edgeList.length && edgeList[index][2] < query[2]; ) {
                    uf.union(edgeList[index][0], edgeList[index][1]);
                    index++;
                }
                ans[queryIndex] = (uf.find(query[0]) == uf.find(query[1]));
            }
            return ans;
        }
    }


    class Solution_Logic_Error {
        public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {
            int[][] graph = new int[n][n];
            for (int[] edge : edgeList) {
                if (graph[edge[0]][edge[1]] == 0 || graph[edge[0]][edge[1]] > edge[2]) {
                    graph[edge[0]][edge[1]] = edge[2];
                    graph[edge[1]][edge[0]] = edge[2];
                }
            }

            boolean[][] visited = new boolean[n][n];
            boolean[] res = new boolean[queries.length];
            for (int i = 0; i < queries.length; i++) {
                res[i] = searchPath(queries[i], graph, visited);
            }

            return res;
        }

        public boolean searchPath(int[] query, int[][] graph, boolean[][] visited) {
            int start = query[0], end = query[1], limit = query[2];
            return dfs(start, end, graph, visited, limit);
        }

        public boolean dfs(int cur, int target, int[][] graph, boolean[][] visited, int limit) {
            if (cur == target) return true;

            for (int next = 0; next < graph.length; next++) {
                if (next == cur || visited[cur][next] || graph[cur][next] == 0 || graph[cur][next] >= limit) continue;
                visited[cur][next] = true;
                visited[next][cur] = true;

                if (dfs(next, target, graph, visited, limit)) return true;

                visited[next][cur] = false;
                visited[cur][next] = false;
            }

            return false;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[][] arr = InputUtil.toDoubleIntegerArray("[[0,1,10],[1,2,5],[2,3,9],[3,4,13]]");
        int[][] queries = InputUtil.toDoubleIntegerArray("[[0,4,14],[1,4,13]]");
        System.out.println(Arrays.toString(solution.distanceLimitedPathsExist(5, arr, queries)));
    }

    public static void main(String[] args) throws Exception {
        LC5632 an = new LC5632();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}

